package com.xinxing.city.recovery;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.xinxing.city.recovery.camea.CameraPreview;
import com.xinxing.city.recovery.camea.FileUtils;
import com.xinxing.city.recovery.interfaces.CameraCallBack;
import com.xinxing.city.recovery.utils.Camera2BackgroundUtil;
import com.xinxing.city.recovery.utils.CameraManager;

import java.io.File;

public class TestActivity extends Activity implements View.OnClickListener, Camera.PictureCallback {

    private Button btOk;
    private FrameLayout frameLayout;
    private CameraPreview cameraPreview;

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        btOk = (Button) findViewById(R.id.take_picture);
        frameLayout = (FrameLayout) findViewById(R.id.camera_content);
        cameraPreview = new CameraPreview(this);
        frameLayout.addView(cameraPreview);
        btOk.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        btOk.setEnabled(false);

        // get an image from the camera
        cameraPreview.takePicture(this);
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File picFile = FileUtils.getFilePath();
        if (picFile == null) {
            Log.d(TAG, "Error creating media file, check storage permissions: ");
            return;
        }
        FileUtils.saveData2Pic(data, picFile);
        // Restart the preview and re-enable the shutter button so that we can take another picture
        camera.startPreview();
        //See if need to enable or not
        btOk.setEnabled(true);
    }

}
